<template>
      <el-upload
        class="avatar-uploader"
        action="http://localhost:5000/goods/goods_img_upload"
        :show-file-list="false"
        :on-success="handleAvatarSuccess">
        <img v-if="imgUrl" :src="`http://localhost:5000/upload/imgs/goods_img/${imgUrl}`" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
</template>

<script>
    /* 
        v-model语法糖原理：
            - 一进
                - model指定一个prop，用于接收外界的数据
                - 在props中定义model指定的属性
            - 一出
                - model指定一个自定义事件，用于将数据发送到外界
                - 在何时的时机，触发model指定的事件
    */
    export default {
         model:{
             prop:'Data',
             event:'change'
         },
         props:{
             Data:String
         },
         data(){
             return {
                 imgUrl:this.Data
             }
         },
         watch:{
             Data:{
                 handler(newVal){
                     this.imgUrl = newVal
                 }
             }
         },
         methods: {
             handleAvatarSuccess(res){
                 this.imgUrl = res.imgUrl
                 this.$emit('change',this.imgUrl)
             }
         },
    }
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>